/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月13日         luwei
 */

package com.coocaa.app.gamecenter.pages.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.RemoteException;
import android.view.View;

import com.coocaa.app.gamecenter.pages.list.GameListActivity;
import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.drawable.ScalingUtils;
import com.skyworth.framework.skysdk.ipc.SkyStartActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.TipToast;

import java.util.HashMap;
import java.util.Map;

public class CategoryItem extends Item {
    private GameCategoryData mCategoryData = null;

    public CategoryItem(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public ItemView createItemView() {
        // TODO Auto-generated method stub
        return new CategoryItemView(mContext);
    }

    @Override
    public void onItemClick(View v) {
        super.onItemClick(v);

        if (LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable)){
            if (mCategoryData.uri != null && !mCategoryData.uri.equals("")
                    && !mCategoryData.uri.equals("null")) {
                LogUtils.i("luwei", "the category uri is " + mCategoryData.uri);
                try {
                    LiteContentServiceConnecter.getService().call(mCategoryData.uri);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            } else {
                Intent intent = new Intent(mContext, GameListActivity.class);
                intent.putExtra(GameListActivity.CATEGORY_ID, mCategoryData.id);
                intent.putExtra(GameListActivity.CATEGORY_NAME, mCategoryData.name);
//                mContext.startActivity(intent);
                HashMap<String,String> map = new HashMap<String,String>();
                SkyStartActivity.startActivity(mContext, mContext.getClass().getName(), intent, map, true);
            }
            Map<String,String> params = new HashMap<String, String>();
            params.put("id",getLogPosition());
            params.put("category","分类页");
            params.put("block_content_name",mCategoryData.name);
            params.put("block_content_id",mCategoryData.id);
            LogUtils.submit(LogUtils.PRODUCTID_APPSTORE, "gc2_main_block_onclick", params);
        }
    }

    private String getLogPosition(){
        int temp = (mIndex + 4)/4;
        int mod = (mIndex)%4 + 1;
        return String.valueOf(temp) + String.valueOf(mod);
    }

    @Override
    public void setData(Object object) {
        this.mCategoryData = (GameCategoryData) object;
        if (mCategoryData.imgurl != null && !mCategoryData.imgurl.equals(""))
            ((CategoryItemView) mItemView).updateIcon(mCategoryData.imgurl);
        super.setData(object);
    }

    public class CategoryItemView extends ItemView {
        private CoocaaDraweeView mIcon = null;

        public CategoryItemView(Context context) {
            super(context);
            addIcon();
        }

        private void addIcon() {
            mIcon = new CoocaaDraweeView(mContext);
            mIcon.setBackgroundResource(R.drawable.game_center_item_bg_shadow);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            mIcon.setLayoutParams(params);
            mIcon.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
            mIcon.getHierarchy().setPlaceholderImage(R.drawable.game_center_default);
            mIcon.setImageResource(R.drawable.game_center_default);
            mIcon.setPadding(Div(14), Div(150), Div(14), Div(24));
            addView(mIcon);
        }

        public void updateIcon(final Bitmap bitmap) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mIcon.setImageBitmap(bitmap);
                }
            });
        }

        @Override
        public void destory() {
            Drawable drawable = mIcon.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            mIcon.setImageDrawable(null);
            mIcon.setBackgroundDrawable(null);
        }

        public void updateIcon(String url) {
            mIcon.setImageURI(Uri.parse(url));
        }

    }

    @Override
    public void destory() {
        mItemView.destory();
        super.destory();
    }

}
